package com.css.fxfzypg.modules.rating.library.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.rating.library.model.dto.KpExamScoringDbInsertIndexDto;
import com.css.fxfzypg.modules.rating.library.model.dto.KpExamScoringDbNewInsertIndexDto;
import com.css.fxfzypg.modules.rating.library.model.dto.KpUpdatePtgxTypeDto;
import com.css.fxfzypg.modules.rating.library.model.vo.GetIndexListByKesdIdVo;
import com.css.fxfzypg.modules.rating.library.model.vo.GetLatestUnsaveIndexVo;
import com.css.fxfzypg.modules.rating.library.service.KpExamScoringDbNewService;
import com.css.fxfzypg.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考评库新建，管理接口
 */
@RestController
@Slf4j
@RequestMapping("kpExamScoringDb/new")
public class KpExamScoringDbNewController {

    @Autowired
    KpExamScoringDbNewService kpExamScoringDbNewService;

    /**
     * 根据考评库id查询指标列表
     *
     * @return
     */
    @GetMapping("getLatestUnsaveIndex")
    public RestResponse<GetLatestUnsaveIndexVo> getLatestUnsaveIndex() {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            return RestResponse.succeed(kpExamScoringDbNewService.getLatestUnsaveIndex(currUid));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 删除考评库
     *
     * @return
     */
    @GetMapping("deleteKpk")
    public RestResponse<String> deleteKpk() {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            kpExamScoringDbNewService.deleteKpk(currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 删除指标接口
     *
     * @return
     */
    @GetMapping("deleteIndexById")
    public RestResponse<String> deleteIndexById(@RequestParam String index3Id) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            kpExamScoringDbNewService.deleteIndexById(index3Id, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 提交考评库接口
     *
     * @param name 考评库名称
     * @return
     */
    @GetMapping("commit")
    public RestResponse<String> commit(@RequestParam String name) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            kpExamScoringDbNewService.commit(name, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 应用指标接口
     *
     * @return
     */
    @PostMapping("applyIndex")
    public RestResponse<KpExamScoringDbNewInsertIndexDto> applyIndex(@RequestBody KpExamScoringDbNewInsertIndexDto dto) {
        try {
            log.debug("param:{}", JSONObject.toJSONString(dto));
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            return RestResponse.succeed(kpExamScoringDbNewService.applyIndex(dto, currUid));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 修改指标的平台更新类型
     *
     * @param dto
     * @return
     */
    @PostMapping("updatePtgxType")
    public RestResponse<String> updatePtgxType(@RequestBody KpUpdatePtgxTypeDto dto) {
        try {
            log.debug("param:{}", JSONObject.toJSONString(dto));
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            kpExamScoringDbNewService.updatePtgxType(dto, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }
}
